from flask import current_app
from flask import Blueprint,  request, jsonify,render_template,session
from bt import Bt_api
from decorator import login_required
# 移除缓存导入，因为我们不再使用缓存

blueprint = Blueprint('/ssl', __name__, url_prefix='/ssl')

@blueprint.route("/", endpoint='/', methods=['GET', 'POST'])
@login_required
def ssl():
    cdn=current_app.config['CDN']
   

    return render_template('ssl.html',cdn=cdn)


# 获取ssl状态
@blueprint.route("/GetSSL", endpoint='/GetSSL', methods=['GET', 'POST'])
@login_required
def GetSSL():
    if request.method == 'POST':
        # 移除缓存相关代码，直接从宝塔API获取数据
    
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.GetSSL(session.get('bt_web'))
        
        # 移除缓存存储代码，直接返回数据
        return jsonify(data)
    

# s申请ssl证书
@blueprint.route("/apply_cert_api", endpoint='/apply_cert_api', methods=['GET', 'POST'])
@login_required
def apply_cert_api():
    if request.method == 'POST':
        # 移除缓存删除代码，因为我们不再使用缓存
        
        domain = request.form.get('domain')
        ssl='["' + domain + '"]'

        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.apply_cert_api(ssl,session.get('siteId'))
        return jsonify(data)
    return jsonify({"code":1,"msg":"error"})

# ssl申请状态
@blueprint.route("/get_ssl_status", endpoint='/get_ssl_status', methods=['GET', 'POST'])
@login_required
def get_ssl_status():
    if request.method == 'POST':
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.get_lines()
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.SetSSL(session.get('bt_web'),data['private_key'],data['cert'])
        
        # 移除缓存删除代码，因为我们不再使用缓存
        
        return jsonify(data['msg'])
    

# 保存ssl
@blueprint.route("/setssl", endpoint='/setssl', methods=['GET', 'POST'])
@login_required
def setssl():
    if request.method == 'POST':
        # 移除缓存删除代码，因为我们不再使用缓存
        
        key = request.form.get('key')
        csr = request.form.get('csr')
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.SetSSL(session.get('bt_web'),key,csr)
        return jsonify(data)

# 续签证书
@blueprint.route("/renew_cert", endpoint='/renew_cert', methods=['GET', 'POST'])
@login_required
def renew_cert():
    if request.method == 'POST':
        # 移除缓存删除代码，因为我们不再使用缓存
        
        index = request.form.get('index')
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.renew_cert(index)
        return jsonify(data)
    

# 关闭证书
@blueprint.route("/CloseSSLConf", endpoint='/CloseSSLConf', methods=['GET', 'POST'])
@login_required
def CloseSSLConf():
    if request.method == 'POST':
        # 移除缓存删除代码，因为我们不再使用缓存
        
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.CloseSSLConf(session.get('bt_web'))
        return jsonify(data)
    
# 关闭强制https
@blueprint.route("/CloseToHttps", endpoint='/CloseToHttps', methods=['GET', 'POST'])
@login_required
def CloseToHttps():
    if request.method == 'POST':
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.CloseToHttps(session.get('bt_web'))
        return jsonify(data)

# 开启强制https
@blueprint.route("/HttpToHttps", endpoint='/HttpToHttps', methods=['GET', 'POST'])
@login_required
def CloseToHttps():
    if request.method == 'POST':
        bt = Bt_api(session.get('bt_url'),session.get('bt_key'))
        data=bt.HttpToHttps(session.get('bt_web'))
        return jsonify(data)